 
import { APollingTask, CacheData } from "../../PollingStation";
export const SitePriority: Record<string,number> = {
    [STRUCTURE_SPAWN]:1,
    [STRUCTURE_TERMINAL]:2,
    [STRUCTURE_LINK]:3,

    [STRUCTURE_EXTENSION]:4,
    [STRUCTURE_CONTAINER]:4,
    [STRUCTURE_TOWER]:5,

    [STRUCTURE_STORAGE]:5,


    [STRUCTURE_LAB]:4,

    [STRUCTURE_ROAD]:10,
    [STRUCTURE_RAMPART]:7,
    [STRUCTURE_FACTORY]:10,
    [STRUCTURE_OBSERVER]:10,
    [STRUCTURE_POWER_SPAWN]:10,
    [STRUCTURE_NUKER]:10,
    [STRUCTURE_WALL]:10,

};
export class CacheDataConstructureSite<T extends ICacheConstructureSite=ICacheConstructureSite> extends CacheData<T>
{
    private structureTypeMap:Record<string,T[]>={};

    constructor(data:T[])
    {
        super(data);

    }

    Init(strcutres:ConstructionSite[])
    {
        const cachedata:T[]=[];
        this.structureTypeMap={};
        Memory.Debug[`CacheConstructionSite  length`]=strcutres.length;
        for(const item of strcutres)
        {

            const ret:T= {
                    id:item.id as string,
                    pos:item.pos,
                    structureType:item.structureType,

                } as T;
                cachedata.push(ret);

            // if( item.structureType == STRUCTURE_ROAD)
            // {
            //     continue;
            // }
            if(this.structureTypeMap[item.structureType])
            {
                this.structureTypeMap[item.structureType].push(ret);

            }
            else
            {
                this.structureTypeMap[item.structureType]=[ret];
            }
        }
        this.cacheData = _.sortBy(cachedata,e=>SitePriority[e.structureType]?SitePriority[e.structureType]:100);
    }
    getStructureSiteCache(type:StructureConstant):T[]|undefined
    {
        if(this.structureTypeMap[type])
        {
            return   this.structureTypeMap[type];
        }
        return undefined

    }
    getStructureSite(type:StructureConstant):ConstructionSite[]|undefined
    {
        const sites:ConstructionSite[]=[];
        if(this.structureTypeMap[type])
        {
            for(const e of this.structureTypeMap[type])
            {
                const obj = Game.getObjectById(e.id as Id<ConstructionSite>);
                if(obj)
                {
                    sites.push(obj);
                }
            }
            return   sites;
        }
        return undefined

    }
    getConstructureSiteCaches()
    { 
        return this.cacheData
    }
    
}

export class CacheConstructureSiteTask<T = CacheDataConstructureSite<ICacheConstructureSite>> extends APollingTask<T>
{
    // roomName!:string;

    Update(...arg:any[]): void
    {
        // this.roomName = arg[0];
        const room = Game.rooms[this.code];
        if(room)
        {
            Memory.Debug[`CacheConstructureSiteTask`]=Game.time;
            const structrures = room.find(FIND_CONSTRUCTION_SITES);
            if(!this.value)
            {
                const cachestructure    =new CacheDataConstructureSite<ICacheConstructureSite>([]);
                this.value =cachestructure as unknown as T;
            }
            (this.value as any).Init(structrures);
            this.lastupdate = Game.time + 37;
        }
    }

}
